﻿// ******************************************************
// Copyright © 2010 DCOM Productions®, 
// All Rights Reserved
// ******************************************************

namespace BitFlex.EnterpriseServices.WindowsShell {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    using System.Diagnostics;
    using BitFlex.EnterpriseServices.Input;
    using System.Windows.Forms;

    /// <summary>
    /// Provides methods and routines to enforce actions on various keys and key combinations within Windows
    /// </summary>
    public static class WindowsKeyService {

        #region Fields

        /// <devdoc>
        /// System.Boolean value indicating whether the Control key is pressed
        /// </devdoc>
        private static bool m_ControlKeyPressed = false;

        /// <devdoc>
        /// System.Boolean value indicating whether the Alt key is pressed
        /// </devdoc>
        private static bool m_AltKeyPressed = false;

        #endregion

        #region HookService Events

        private static void AltF4_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyValue == 164 || e.KeyValue == 165) {
                m_AltKeyPressed = true;
            }
            if (e.KeyCode == Keys.F4) {
                e.SuppressKeyPress = true;
            }
        }

        private static void AltF4_KeyUp(object sender, KeyEventArgs e) {
            if (m_AltKeyPressed) {
                if (e.KeyValue == 164 || e.KeyValue == 165) {
                    m_AltKeyPressed = false;
                }
            }
        }

        private static void AltTab_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyValue == 164 || e.KeyValue == 165) {
                m_AltKeyPressed = true;
            }
            if (e.KeyCode == Keys.Tab) {
                e.SuppressKeyPress = true;
            }
        }

        private static void AltTab_KeyUp(object sender, KeyEventArgs e) {
            if (m_AltKeyPressed) {
                if (e.KeyValue == 164 || e.KeyValue == 165) {
                    m_AltKeyPressed = false;
                }
            }
        }

        private static void ControlEscape_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyValue == 162 || e.KeyValue == 163) {
                m_ControlKeyPressed = true;
            }
            if (m_ControlKeyPressed) {
                if (e.KeyCode == Keys.Escape) {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private static void ControlEscape_KeyUp(object sender, KeyEventArgs e) {
            if (m_ControlKeyPressed) {
                 if (e.KeyValue == 162 || e.KeyValue == 163) {
                    m_ControlKeyPressed = false;
                }
            }
        }

        private static void WinKey_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin) {
                e.SuppressKeyPress = true;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Disables the ALT+F4 keyboard combination
        /// </summary>
        public static void DisableAltF4() {
            HookService.KeyDown += AltF4_KeyDown;
            HookService.KeyUp += AltF4_KeyUp;
        }

        /// <summary>
        /// Disables the ALT+TAB keyboard combination
        /// </summary>
        public static void DisableAltTab() {
            HookService.KeyDown += AltTab_KeyDown;
            HookService.KeyUp += AltTab_KeyUp;
        }

        /// <summary>
        /// Disables the CTRL+ESC keyboard combination
        /// </summary>
        public static void DisableControlEscape() {
            HookService.KeyDown += ControlEscape_KeyDown;
            HookService.KeyUp -= ControlEscape_KeyUp;
        }

        /// <summary>
        /// Disables the Windows Key using a Low-Level System Hook
        /// </summary>
        public static void DisableWinKey() {
            HookService.KeyDown += WinKey_KeyDown;
        }

        /// <summary>
        /// Enables the ALT+F4 keyboard combination
        /// </summary>
        public static void EnableAltF4() {
            HookService.KeyDown -= AltF4_KeyDown;
            HookService.KeyUp -= AltF4_KeyUp;
        }

        /// <summary>
        /// Enables the ALT+TAB keyboard combination
        /// </summary>
        public static void EnableAltTab() {
            HookService.KeyDown -= AltTab_KeyDown;
            HookService.KeyUp -= AltTab_KeyUp;
        }

        /// <summary>
        /// Enables the CTRL+ESC keyboard combination
        /// </summary>
        public static void EnableControlEscape() {
            HookService.KeyDown -= ControlEscape_KeyDown;
            HookService.KeyUp -= ControlEscape_KeyUp;
        }

        /// <summary>
        /// Enables the Windows Key using a Low-Level System Hook
        /// </summary>
        public static void EnableWinKey() {
            HookService.KeyDown -= WinKey_KeyDown;
        }

        #endregion
    }
}